#!/usr/bin/python
# -*- coding: UTF-8 -*-

import MySQLdb
import sys

class DbHelper():
	def __init__(self):
		#写入新番列表指令
		self.insertSeasonSQL = "INSERT INTO season (name, episode, day_of_week, delay, season_status, url, smallurl, pl) VALUES ('%s','%s','%d','%d','%d','%s','%s','%d');"
		self.insertUserSQL = "INSERT INTO wxuser (user_id, follow_info) VALUES ('%s','%s');"
		print('connect database...')
		self.db = MySQLdb.connect(host="localhost",user="root",passwd="1234",db="otaku",port=3308,charset="utf8")
		self.cur = self.db.cursor()
	
	#写入一条新番数据
	def insert_season(self,name,episode,week,delay,season_status,url,smallurl,pl):
		self.cur.execute(self.insertSeasonSQL % (name,episode,week,delay,season_status,url,smallurl,pl))
		self.db.commit()

	#更新新番数据
	def updata_season(self,name,episode,week,delay,season_status,url,smallurl,pl):
		if self.select_season(name) == None:
			self.insert_season(name,episode,week,delay,season_status,url,smallurl,pl)
		self.cur.execute("UPDATE season SET episode = '%s', season_status = '%d' WHERE name = '%s';" % (episode,season_status,name))
		self.db.commit()

	#获取是否存在
	def select_season(self,seasonName):
		result = self.cur.execute("SELECT * FROM season WHERE name = '%s';" % (seasonName))
		if result == 0:
			return None
		else:
			info = self.cur.fetchone()
			return info

	#新建用户数据
	def insert_user(self,user_id):
		self.cur.execute(self.insertUserSQL % (user_id,''))
		self.db.commit()

	#更新用户数据
	def updata_user(self,user_id,info):
		if self.select_user(user_id) == None:
			self.insert_user(user_id)
		self.cur.execute("UPDATE wxuser SET follow_info = '%s' WHERE user_id = '%s';" % (info,user_id))
		self.db.commit()

	#获取用户是否存在
	def select_user(self,user_id):
		result = self.cur.execute("SELECT * FROM wxuser WHERE user_id = '%s';" % (user_id))
		if result == 0:
			return None
		else:
			info = self.cur.fetchone()
			return info

dbHelper = DbHelper()
#dbHelper.updata_season('One Room 第二季','第47话',3,0,'http://i0.hdslb.com/bfs/bangumi/cc27b47f07d14e3de0f6f7d60ede625fb9ae9727.png','http://i0.hdslb.com/bfs/bangumi/1ab963760136215c8bdc85373aa3ab87d9873018.jpg')
#dbHelper.select_season('aaa')
#print(dbHelper.check_image_exist(1))
# dbHelper.insert_tag('fff')
# print(dbHelper.check_tag_exist('fff'))
# dbHelper.insert_taglink(11,'asd')